package com.ship.dispatch.service.fob.impl;

import cn.hutool.json.ObjectMapper;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.utils.DateUtil;
import com.ship.common.core.utils.HttpRequestUtil;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.dispatch.bean.SpArrangeSchemeDetail;
import com.ship.dispatch.bean.SpDispatchSchemeDetail;
import com.ship.dispatch.bean.SpDispatchSchemeExecute;
import com.ship.dispatch.bean.fob.*;
import com.ship.dispatch.bean.orderPay.ConThirdPartyMeasurement;
import com.ship.dispatch.bo.shipBusiness.ThirdPartyMeasureBo;
import com.ship.dispatch.bo.shipBusiness.ThirdPartyMeasureRecordBo;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.bpm.utils.TemplateParamUtils;
import com.ship.dispatch.bpm.vo.ManageEntityVo;
import com.ship.dispatch.mapper.fob.SpFobSyzlzzwjMapper;
import com.ship.dispatch.mapper.orderPay.ConThirdPartyMeasurementMapper;
import com.ship.dispatch.service.SpArrangeSchemeDetailService;
import com.ship.dispatch.service.SpDispatchSchemeDetailService;
import com.ship.dispatch.service.SpDispatchSchemeExecuteService;
import com.ship.dispatch.service.fob.SpFobFileRecordService;
import com.ship.dispatch.service.fob.SpFobSyzlzzwjService;
import com.ship.dispatch.service.fob.SpFobTemplateService;
import com.ship.dispatch.service.fob.SpFobZddsfjlService;
import com.ship.dispatch.service.shipBusiness.SmThirdPartyMeasureService;
import com.ship.dispatch.util.PropertiesUtil;
import com.ship.dispatch.util.StringUtils;
import com.ship.dispatch.vo.shipReport.SynchronThirdPartyMeasureVo;
import liquibase.pro.packaged.B;
import liquibase.pro.packaged.L;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yl
 * @title SpFobSyzlzzwjServiceImpl
 * @description 审阅并整理装载文件
 * @create 2023/12/18 0018
 */
@Service
public class SpFobSyzlzzwjServiceImpl extends FobBaseServiceImpl<SpFobSyzlzzwjMapper,SpFobSyzlzzwj> implements SpFobSyzlzzwjService {

    @Autowired(required = false)
    SpFobSyzlzzwjMapper mapperapper;

    @Autowired
    SpFobFileRecordService spFobFileRecordService;

    @Autowired
    SpDispatchSchemeExecuteService schemeDetailService;

    @Autowired
    SpFobTemplateService spFobTemplateService;
    @Autowired
    SpArrangeSchemeDetailService spArrangeSchemeDetailService;

    @Autowired
    SpDispatchSchemeExecuteService spDispatchSchemeExecuteService;

    @Autowired
    @Lazy
    private SpDispatchSchemeDetailService spDispatchSchemeDetailService;

    private final AsyncService asyncService;

    public SpFobSyzlzzwjServiceImpl(AsyncService asyncService) {
        this.asyncService = asyncService;
    }
    @Override
    public void updateEntity(ManageEntityVo manageEntityVo, String json) {
        SpFobSyzlzzwj object = JsonUtils.jsonToObject(json,SpFobSyzlzzwj.class);
        SpFobSyzlzzwj his = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlzzwj>().eq(SpFobSyzlzzwj::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpFobSyzlzzwj::getStatus,-2));
        if(manageEntityVo.getOperationType()==1) {
            object.setStatus(1);
        }
        if(his==null){ // 新增
            object.setSchemeDetailId(manageEntityVo.getSchemeDetailId());
            object.setTaskId(manageEntityVo.getTaskId());
            object.setProceeInstanceId(manageEntityVo.getProcessInstanceId());
            mapperapper.insert(object);
        }else { // 修改
            object.setId(his.getId());
            if((his.getSgs()==null && object.getSgs()!=null) || (his.getSgs()!=null && object.getSgs()!=null && !his.getSgs().equals(object.getSgs()))){
                object.setField3("-1");
                asyncService.executeSyzlzzwj(his.getId(),object.getSgs());
                /*if(reportObject!=null){
                    try {
                        PropertiesUtil.copyProperties(reportObject,object);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }*/
            }
            BeanUtils.copyProperties(object,his);
            //是否更新记录数据

            mapperapper.updateById(his);
            // 处理多余文件记录
            his = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlzzwj>().eq(SpFobSyzlzzwj::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpFobSyzlzzwj::getStatus,-2));

            List<String> ids = new ArrayList<>();
            if((his.getFiels()!=null && his.getFiels().length()>0)){
                String temp = his.getFiels();
                String[] fieldIds = temp.split(",");
                List<String> templIds =  Arrays.stream(fieldIds).collect(Collectors.toList());
                ids.addAll(templIds);
                //spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
            }
            if((his.getSgs()!=null && his.getSgs().length()>0)){
                String temp = his.getSgs();
                String[] fieldIds = temp.split(",");
                List<String> templIds =  Arrays.stream(fieldIds).collect(Collectors.toList());
                ids.addAll(templIds);
                //spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
            }
            if((his.getCommunicateEmail()!=null && his.getCommunicateEmail().length()>0)){
                String temp = his.getCommunicateEmail();
                String[] fieldIds = temp.split(",");
                List<String> templIds =  Arrays.stream(fieldIds).collect(Collectors.toList());
                ids.addAll(templIds);
                //spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
            }
            if((his.getZzFile()!=null && his.getZzFile().length()>0)){
                String temp = his.getZzFile();
                String[] fieldIds = temp.split(",");
                List<String> templIds =  Arrays.stream(fieldIds).collect(Collectors.toList());
                ids.addAll(templIds);
            }
            spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
            // 解析文档信息数据

        }
        if(object.getLoadingStartDate()!=null) { // 完成任务 更新方案表实际装载实际
            SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(manageEntityVo.getProcessInstanceId());
            if(execute!=null){
                SpArrangeSchemeDetail spDispatchSchemeDetail = new SpArrangeSchemeDetail();
                spDispatchSchemeDetail.setId(execute.getArrangeSchemeDetailId());
                spDispatchSchemeDetail.setRealityLoadingDate(object.getLoadingStartDate());
                if(his.getWeight()!=null){
                    spDispatchSchemeDetail.setRealityLoadingCapacity(new BigDecimal(his.getWeight()));
                }
                spArrangeSchemeDetailService.updateById(spDispatchSchemeDetail);
                spDispatchSchemeDetailService.update(
                        new LambdaUpdateWrapper<SpDispatchSchemeDetail>()
                                .set(SpDispatchSchemeDetail::getRealityLoadingDate,object.getOnLoadDate())
                                .eq(SpDispatchSchemeDetail::getArrangeSchemeDetailId,execute.getArrangeSchemeDetailId())
                );
                execute.setRealityLoadingDate(object.getLoadingStartDate());
                schemeDetailService.updateById(execute);
            }
        }
    }



    @Resource
    HttpRequestUtil httpRequestUtil;
    /**
     *
     * 解析计量报告文档信息
     * @param fileId
     * @return
     */
    @Async
    public void relaxJLReport(Long id,String fileId) {
        SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
        if(fileRecord!=null){
            try{
                URL url = new URL(fileRecord.getPath());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                InputStream in = connection.getInputStream();
                if(in!=null){
                    JSONObject infos = httpRequestUtil.ocrJLReport(in,fileRecord.getId()+"");
                    SpFobSyzlzzwj spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobSyzlzzwj.class);
                    spFobSyzlzzwj.setId(id);
                    this.updateById(spFobSyzlzzwj);
                }
            }catch (Exception e){
                log.error("异步解析ocr错误",e);
            }

        }
        //return  null;
    }

    public static MultipartFile convert(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        InputStream in = connection.getInputStream();

        byte[] body = new byte[in.available()];
        in.read(body);

        MultipartFile multipartFile = new MockMultipartFile("file", "file", "text/plain", body);

        return multipartFile;
    }


    @Resource
    SmThirdPartyMeasureService smThirdPartyMeasureService;
    // 完成任务执行同步信息
    public void createPayOrder(Long id){
        SpFobSyzlzzwj spFobSyzlzzwj = this.getById(id);
        //执行同步逻辑
        Long thirdParty = syncThirdParty(spFobSyzlzzwj);
        spFobSyzlzzwj.setThirdPartyId(thirdParty);
        this.updateById(spFobSyzlzzwj);
        // 更新货号
        if(StringUtils.isNotEmpty(spFobSyzlzzwj.getField3())){
            SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(spFobSyzlzzwj.getProceeInstanceId());
            execute.setGoodsNum(spFobSyzlzzwj.getField3());
            schemeDetailService.updateById(execute);
        }
    }

    @Autowired
    SpFobZddsfjlService spFobZddsfjlService;

    @Autowired(required = false)
    ConThirdPartyMeasurementMapper conThirdPartyMeasurementMapper;

    public Long syncThirdParty(SpFobSyzlzzwj spFobSyzlzzwj){
        SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(spFobSyzlzzwj.getProceeInstanceId());
        SpFobZddsfjl spFobZddsfjl = spFobZddsfjlService.findData(spFobSyzlzzwj.getProceeInstanceId());
        ThirdPartyMeasureBo thirdPartyMeasureBo = new ThirdPartyMeasureBo();
        thirdPartyMeasureBo.setLoadingStartDate(spFobSyzlzzwj.getLoadingStartDate());
        thirdPartyMeasureBo.setLoadingEndDate(spFobSyzlzzwj.getLoadingStartDate());
        //thirdPartyMeasureBo.ser
        thirdPartyMeasureBo.setVoyageType(1);
        if(execute!=null){
            thirdPartyMeasureBo.setVoyage(execute.getVoyageNo());
            thirdPartyMeasureBo.setLoadingPort(execute.getExpectedLoadingPort());
        }
        if(spFobZddsfjl!=null && spFobZddsfjl.getCronId()!=null){
            ConThirdPartyMeasurement conThirdPartyMeasurement = conThirdPartyMeasurementMapper.selectById(spFobZddsfjl.getCronId());
            if(conThirdPartyMeasurement!=null){
                thirdPartyMeasureBo.setLoadingIndependent(conThirdPartyMeasurement.getPartyB());
            }
        }
        //Gross Quantity
        thirdPartyMeasureBo.setLoadingGrossMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGrossQuantityMmbtu()));
        thirdPartyMeasureBo.setLoadingGrossTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGrossQuantityM3()));
        thirdPartyMeasureBo.setLoadingGrossMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGrossQuantityMt()));

        thirdPartyMeasureBo.setLoadingNetMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getNetQuantityMmbtu()));
        thirdPartyMeasureBo.setLoadingNetTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getNetQuantityM3()));
        thirdPartyMeasureBo.setLoadingNetMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getNetQuantityMt()));

        thirdPartyMeasureBo.setLoadingGhv(getStringToBigDecimal(spFobSyzlzzwj.getGhv()));
        thirdPartyMeasureBo.setLoadingTemperature(getStringToBigDecimal(spFobSyzlzzwj.getTemperature()));
        thirdPartyMeasureBo.setLoadingDensity(getStringToBigDecimal(spFobSyzlzzwj.getDensity()));
        thirdPartyMeasureBo.setLoadingGas(getStringToBigDecimal(spFobSyzlzzwj.getGasificationRate()));

        thirdPartyMeasureBo.setLoadingCoolMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getCoolDownMmbtu()));
        thirdPartyMeasureBo.setLoadingCoolTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getCoolDownM3()));
        thirdPartyMeasureBo.setLoadingCoolMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getCoolDownMt()));

        thirdPartyMeasureBo.setLoadingVapourMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getVapourMmbtu()));
        thirdPartyMeasureBo.setLoadingVapourMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getVapourMt()));
        thirdPartyMeasureBo.setLoadingVapourTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getVapourM3()));

        thirdPartyMeasureBo.setLoadingConsumedMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getBogConsumedMmbtu()));
        thirdPartyMeasureBo.setLoadingConsumedMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getBogConsumedMt()));
        thirdPartyMeasureBo.setLoadingConsumedTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getBogConsumedM3()));

        thirdPartyMeasureBo.setLoadingGassingMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGassingUpMmbtu()));
        thirdPartyMeasureBo.setLoadingGassingMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGassingUpMt()));
        thirdPartyMeasureBo.setLoadingGassingTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGassingUpM3()));

        if(spFobSyzlzzwj.getComponent()!=null && spFobSyzlzzwj.getComponent()!=""){
            try {
                //解析json数组
                List<JSONObject> list = JsonUtils.jsonToList(spFobSyzlzzwj.getComponent(),JSONObject.class);
                for(int i=0;i<list.size();i++){
                    JSONObject object = new JSONObject(list.get(i));
                    String name = object.getString("name");
                    String value = object.getString("value");
                    if(name.contains("Methane")){
                        thirdPartyMeasureBo.setLoadingMethane(getStringToBigDecimal(value));
                    }else if(name.contains("Ethane")){
                        thirdPartyMeasureBo.setLoadingEthane(getStringToBigDecimal(value));
                    }else if(name.contains("Propane")){
                        thirdPartyMeasureBo.setLoadingPropane(getStringToBigDecimal(value));
                    } else if (name.contains("lso Butane")) {
                        thirdPartyMeasureBo.setLoadingIbutane(getStringToBigDecimal(value));
                    } else if (name.contains("N-Butane")) {
                        thirdPartyMeasureBo.setLoadingNbutane(getStringToBigDecimal(value));
                    } else if (name.contains("lso Pentane")) {
                        thirdPartyMeasureBo.setLoadingIpentane(getStringToBigDecimal(value));
                    } else if (name.contains("N-Pentane")) {
                        thirdPartyMeasureBo.setLoadingNpentane(getStringToBigDecimal(value));
                    }else if (name.contains("N-Hexane")){
                        thirdPartyMeasureBo.setLoadingNhexane(getStringToBigDecimal(value));
                    }else if(name.contains("Nitrogen")){
                        thirdPartyMeasureBo.setLoadingNitrogen(getStringToBigDecimal(value));
                    } else if (name.contains("Oxygen")) {
                        thirdPartyMeasureBo.setLoadingOxygen(getStringToBigDecimal(value));
                    }else if (name.contains("Carbon Dioxide")){
                        thirdPartyMeasureBo.setLoadingDioxide(getStringToBigDecimal(value));
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        SynchronThirdPartyMeasureVo vo = smThirdPartyMeasureService.saveSmThirdPartyMeasure(thirdPartyMeasureBo);
        if(spFobSyzlzzwj.getEvent()!=null && spFobSyzlzzwj.getEvent()!=""){
            try {
                List<JSONObject> list = JsonUtils.jsonToList(spFobSyzlzzwj.getEvent(),JSONObject.class);
                for(int i=0;i<list.size();i++){
                    JSONObject object = new JSONObject(list.get(i));
                    ThirdPartyMeasureRecordBo thirdPartyMeasureRecordBo = new ThirdPartyMeasureRecordBo();
                    thirdPartyMeasureRecordBo.setRecordContent(object.getString("event"));
                    if(object.get("date")!=null && object.getString("event")!=null){
                        thirdPartyMeasureRecordBo.setRecordTime(object.getString("date"));
                        thirdPartyMeasureRecordBo.setRecordType(1);
                        thirdPartyMeasureRecordBo.setThirdPartyMeasureId(vo.getId());
                        smThirdPartyMeasureService.saveSmThirdPartyMeasureRecord(thirdPartyMeasureRecordBo);
                    }

                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
        //新增明细
        return vo.getId();
    }

    public BigDecimal getStringToBigDecimal(String num){
        if(StringUtils.isNotEmpty(num)){
            try{
                return new BigDecimal(num);
            }catch (Exception e){
                return null;
            }
        }
        return null;
    }


    @Override
    public SpFobBaseBean getBaseBeanByInstanceId(String id) {
        return mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlzzwj>().eq(SpFobSyzlzzwj::getProceeInstanceId,id).ne(SpFobSyzlzzwj::getStatus,-2));
    }
    @Override
    public void  saveEntity(SpFobBaseBean bean){
        SpFobSyzlzzwj spFob = new SpFobSyzlzzwj();
        BeanUtils.copyProperties(bean,spFob);
        this.save(spFob);
    }

    @Override
    public void updateEntityById(SpFobBaseBean bean) {
        SpFobSyzlzzwj spFob = new SpFobSyzlzzwj();
        BeanUtils.copyProperties(bean,spFob);
        this.updateById(spFob);
    }

    

    @Override
    public Object getFormData(String id,Long serviceId) {
        SpFobSyzlzzwj spFobZzgcjr = null;
        if(serviceId!=null){
            spFobZzgcjr = mapperapper.selectById(serviceId);
        }else {
            spFobZzgcjr = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlzzwj>().eq(SpFobSyzlzzwj::getProceeInstanceId,id).ne(SpFobSyzlzzwj::getStatus,-2));
        }

        if(spFobZzgcjr==null){
            //spFobZzgcjr = new SpFobSyzlzzwj();
           return null;
        }
        Map<String,Object> map = JsonUtils.jsonToMap(JsonUtils.objectToJson(spFobZzgcjr), Object.class);
        if(StringUtils.isNotBlank(spFobZzgcjr.getFiels())){
            String[] ids = spFobZzgcjr.getFiels().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("fiels",spFobFileRecordList);
        }
        if(spFobZzgcjr.getZzFile()!=null){
            String[] ids = spFobZzgcjr.getZzFile().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("zzFile",spFobFileRecordList);
        }
        if(spFobZzgcjr.getSgs()!=null){
            String[] ids = spFobZzgcjr.getSgs().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("sgs",spFobFileRecordList);
        }
        if(spFobZzgcjr.getCommunicateEmail()!=null){
            String[] ids = spFobZzgcjr.getCommunicateEmail().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("communicateEmail",spFobFileRecordList);
        }
        if(!map.containsKey("component") || map.get("component")==null){
            //初始化component数据
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name","Methane (CH4)");
            jsonObject.put("value","");
            jsonObject.put("unit","%Mol");
            jsonArray.add(jsonObject);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name","Ethane (C2H6)");
            jsonObject1.put("value","");
            jsonObject1.put("unit","%Mol");
            jsonArray.add(jsonObject1);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("name","Propane (C3H8)");
            jsonObject2.put("value","");
            jsonObject2.put("unit","%Mol");
            jsonArray.add(jsonObject2);
            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("name","lso Butane (i-C4H10)");
            jsonObject3.put("value","");
            jsonObject3.put("unit","%Mol");
            jsonArray.add(jsonObject3);
            JSONObject jsonObject4 = new JSONObject();
            jsonObject4.put("name","N-Butane(n-C4H10)");
            jsonObject4.put("value","");
            jsonObject4.put("unit","%Mol");
            jsonArray.add(jsonObject4);
            JSONObject jsonObject5 = new JSONObject();
            jsonObject5.put("name","lso Pentane (i-C5H12)");
            jsonObject5.put("value","");
            jsonObject5.put("unit","%Mol");
            jsonArray.add(jsonObject5);
            JSONObject jsonObject6 = new JSONObject();
            jsonObject6.put("name"," N-Pentane (n-C5H12)");
            jsonObject6.put("value","");
            jsonObject6.put("unit","%Mol");
            jsonArray.add(jsonObject6);
            JSONObject jsonObject7 = new JSONObject();
            jsonObject7.put("name"," N-Hexane (n-C6H14)");
            jsonObject7.put("value","");
            jsonObject7.put("unit","%Mol");
            jsonArray.add(jsonObject7);
            JSONObject jsonObject8 = new JSONObject();
            jsonObject8.put("name"," Nitrogen (N2)");
            jsonObject8.put("value","");
            jsonObject8.put("unit","%Mol");
            jsonArray.add(jsonObject8);
            JSONObject jsonObject9 = new JSONObject();
            jsonObject9.put("name","Oxygen (02)");
            jsonObject9.put("value","");
            jsonObject9.put("unit","%Mol");
            jsonArray.add(jsonObject9);
            JSONObject jsonObject10 = new JSONObject();
            jsonObject10.put("name","Carbon Dioxide (C02)");
            jsonObject10.put("value","");
            jsonObject10.put("unit","%Mol");
            jsonArray.add(jsonObject10);
            map.put("component",JsonUtils.objectToJson(jsonArray));
        }
        if(!map.containsKey("event") || map.get("event")==null) {
            //初始化component数据
            JSONArray jsonArray = new JSONArray();
            map.put("event",JsonUtils.objectToJson(jsonArray));
        }
        SpDispatchSchemeExecute schemeDetail = schemeDetailService.getExceuteByInstanceId(spFobZzgcjr.getProceeInstanceId());
        if(schemeDetail!=null && (!map.containsKey("field2") || map.get("field2")==null)){
            map.put("field2",schemeDetail.getGoodsNum());
        }
        SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(spFobZzgcjr.getProceeInstanceId());
        if (execute.getExpectedLoadingPort()!=null && spFobZzgcjr.getPort() == null){
            map.put("port",execute.getExpectedLoadingPort());
        }
        return map;
    }

    @Override
    public List<Long> findServieData(String processInstanceId){
        List<SpFobSyzlzzwj> spFobBaseBeans =  mapperapper.selectList(new LambdaQueryWrapper<SpFobSyzlzzwj>().eq(SpFobSyzlzzwj::getProceeInstanceId,processInstanceId).orderByDesc(SpFobSyzlzzwj::getCreateDate));
        List<Long> list = spFobBaseBeans.stream().map(SpFobBaseBean::getId).collect(Collectors.toList());
        return list;
    }

    @Override
    public SpFobSyzlzzwj findActivitiData(String processInstanceId) {
        SpFobSyzlzzwj spFobZzgcjr = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlzzwj>().eq(SpFobSyzlzzwj::getProceeInstanceId,processInstanceId).ne(SpFobSyzlzzwj::getStatus,-2));
        return spFobZzgcjr;
    }
}
